public class Solution567 {
    public boolean checkInclusion(String s1, String s2) {
        if (s1.length()>s2.length()){
            return false;
        }
        int[] f=new int[26];
        int[] g=new int[26];
        int d=0;
        for (int i=0;i<s1.length();i++){
            f[s1.charAt(i)-'a']++;
            g[s2.charAt(i)-'a']++;
        }
        for (int i = 0; i < 26; i++) {
            if (f[i]!=g[i]){
                d++;
            }
        }
        if (d==0){
            return true;
        }
        for (int i=s1.length();i<s2.length();i++){
            int t1=s2.charAt(i-s1.length())-'a';
            g[t1]--;
            if (g[t1]==f[t1]){
                d--;
            }else{
                if (g[t1]+1==f[t1]){
                    d++;
                }
            }
            int t2=s2.charAt(i)-'a';
            g[t2]++;
            if (g[t2]==f[t2]){
                d--;
            }else{
                if (g[t2]-1==f[t2]){
                    d++;
                }
            }
            if (d==0){
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        System.out.println(new Solution567().checkInclusion("ab","eidbaooo"));
    }
}
